
(function(){
	//初始化设备状态
	initDeviceState();
	//根据参数设置页面表盘的样式
	function showsyl(){
		var wbiao = $("#myStats");
		var nbiao = $("#myStat2");
		console.log("渲染表盘");
		wbiao.attr("data-percent",model.lel);
		var ydqi = 100*(model.ppm/1000);
		nbiao.attr("data-percent",ydqi);
		var jhrl = "javascript:window.location.href='wx/dj'";
		 var statHtml = "<div class = 'myStats' id='myStats' data-dimension='250' data-text='50%' data-info='空气安全' data-width='15' data-fontsize='38' data-percent='"+model.lel+"' data-fgcolor='rgba(242, 134, 134, 0.5)' data-bgcolor = '#eee'></div><div class = 'myStats' id='myStat2' data-dimension='230' data-text='4%' data-info='空气安全' data-width='5' data-fontsize='38' data-percent='"+ydqi+"' data-fgcolor='rgba(242, 134, 134, 0.5)' data-bgcolor = '#eee'><div class='texts' onclick= "+jhrl+"><p id='showc'>正常</p><p id='pmField'>空气安全</p></div></div>";
	  $('.stat-div').html(statHtml);
	  $('#myStats').circliful();
	  $('#myStat2').circliful();
		if((model.lel<=5&&model.lel>=0)&&(model.ppm<=50&&model.ppm>=0)){
			$("#showc").text("正常");
			$("#pmField").text("空气安全");
			$(".head-group").css({"background":""});			
		}else{
			if(model.lel<=10&&model.ppm<=100){
				$("#showc").text("泄露");
				$("#pmField").text("气体超标");
				$(".head-group").css({"background":"url(aigo/img/lou.png) no-repeat center center","background-size":"100%"});
			}else{
				$("#showc").text("严重");
				$("#pmField").text("超标严重");
				$(".head-group").css({"background":"url(aigo/img/imp.png) no-repeat center center","background-size":"100%"});			
			}
			
		} 
	}
	//根据参数渲染页面气体值
	function showqi(){
		var i = 0;
		var is = 0;
		var icons = setInterval(getnumss, 20);
		var iconss = setInterval(getnumsss, 2);
		function getnumss(){
			if(i>=parseInt(model.lel)||typeof(model.lel)=="undefined"){
				clearInterval(icons);
			}
		if(i<=5){
			$("#krsdj").text("一级");
		}else if((i>5&&i<=10)){
			$("#krsdj").text("二级");
		}else{
			$("#krsdj").text("三级");			
		}
		$("#krs").text(i);
				i++;
		}	
		function getnumsss(){
			if(is>=parseInt(model.ppm)||typeof(model.ppm)=="undefined"){
				clearInterval(iconss);
			}
		$("#yds").text(is);
		if(is<=50){
			$("#ydsdj").text("一级");
		}else if((is>50&&is<=100)){
			$("#ydsdj").text("二级");
		}else{
			$("#ydsdj").text("三级");			
		}					
				is++;
		}				
	}
	//显示设备列表
	function showdevice(){
		console.log("xians");
		$(".operation-device-list").css({"height":"100%"});
		$(".operation-device-list>ul>li,.titles").css({"display":"block"});
	}
	//隐藏设备列表
	function hidedevice(){
		$(".operation-device-list").css({"height":"0%"});
		$(".operation-device-list>ul>li,.titles").css({"display":"none"});
	}
	var modal = $('.dialog-modal');
	var preloaderModal = $('.preloader-modal');


	var timer1,timer2;
	//显示弹框
	function showModal(text){
		if(text){
			$('.modal-text').text(text);
		}
		modal.show();
		showOverlay();
		setTimeout(function() {
			modal.addClass('modal-in').removeClass('modal-out')
		}, 50);
	}

	//隐藏弹框
	function hideModal(){
		modal.addClass('modal-out').removeClass('modal-in');
		hideOverlay();
		setTimeout(function() {
			modal.hide();
		}, 350);
	}

	// 显示加载框
	function showPreloader(text){
		if(text){
			$('.preloader-text').text(text);
		}
		preloaderModal.show();
		showOverlay();
		setTimeout(function() {
			preloaderModal.addClass('modal-in').removeClass('modal-out')
		}, 50);
	}

	//隐藏加载框
	function hidePreloader(){
		preloaderModal.addClass('modal-out').removeClass('modal-in');
		hideOverlay();
		setTimeout(function() {
			preloaderModal.hide();
		}, 350);
	}

	//显示蒙层
	function showOverlay(){
		$('.modal-overlay').addClass('modal-overlay-visible');
	}

	//隐藏蒙层
	function hideOverlay(){
		$('.modal-overlay').removeClass('modal-overlay-visible');
	}


	$('#okBtn').on('click',function(){
		hideModal();
	})

	$('.preloader-close-btn').on('click',function(){
		hidePreloader();
	})
	//促发设备列表
	$("#deviceshow").on('click',function(){
		showdevice();
	})
	$("#deviceq").on('click',function(){
		hidedevice();
	})	
	$(".operation-device-list>ul>li").on('click',function(){
		$(".operation-device-list>ul>li").removeClass("active");
		$(this).addClass("active");
		hidedevice();
	})
	//showPreloader('设备加载中');

	//初始化设备状态
	function initDeviceState(){	
		console.log(model.isOn);
		if(model.isOn){
			setTimeout(function() {
			hidePreloader();
			}, 1000);
			$(".dshow>i").css({'background':''});
			showsyl();
			showqi();
		}else{
			$(".dshow>i").css({'background':'rgb(187, 165, 165)'});
			setTimeout(function() {
			hidePreloader();
			showModal('硬件未联网')
			}, 1000);			
		}
	}
	FastClick.attach(document.body);
})();
